//
//  ProductImageSettingVC.m
//  danpin
//
//  Created by chuangjia on 23/7/15.
//  Copyright (c) 2015 chuangjia. All rights reserved.
//

#import "ProductImageSettingVC.h"
#import "ProductCreateVC.h"
#import "CellConfig.h"
@interface ProductImageSettingVC ()<UITableViewDataSource,UITableViewDelegate>
{
    
}
@property(nonatomic,strong)UITableView *tbView;
@property(nonatomic,strong) NSMutableArray *setcionArray;
@property(nonatomic,strong) NSString *top;
@end

@implementation ProductImageSettingVC
@synthesize type=_type;
@synthesize rank_id=_rank_id;
@synthesize dictionary=_dictionary;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor whiteColor];
    self.rightTitle = @"下一步";
    self.navTitle =@"选择图片";
    //
    if (!_setcionArray) {
        _setcionArray =[[NSMutableArray alloc] init];
    }
    _top = @"0";
    [self getData];
    [self initUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
      DLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)dealloc
{
    [_setcionArray removeAllObjects];
    _setcionArray =nil;
    self.tbView.tableFooterView =nil;
    self.tbView =nil;
    DLog(@"%s", __PRETTY_FUNCTION__);
}
-(void)clickNavEvent:(id)sender
{
    UIButton *btn =(UIButton*)sender;
    switch (btn.tag) {
        case 0:
        {
            self.navigationController.enableBackGesture=YES;
            if (self.navigationController) {
                if (self.navigationController.viewControllers.count == 1) {
                    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                } else {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            } else {
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }
            break;
        case 1:
        {
            [self newAction];
        }
            break;
        default:
            break;
    }
}

-(void)newAction
{
    NSMutableArray *tempArray =[NSMutableArray array];
    for (int i=0;i<[self.setcionArray count];i++) {
        NSDictionary *dict =[self.setcionArray objectAtIndex:i];
        if ([[dict objectForKey:@"isTop"] intValue]==1 || [[dict objectForKey:@"isSelected"] intValue]==1) {
            [tempArray addObject:dict];
        }
    }
    ProductCreateVC *pcreate =[[ProductCreateVC alloc] init];
    pcreate.dictionary =self.dictionary;
    pcreate.top =self.top;
    pcreate.type=self.type;
    pcreate.pictureinfolist = tempArray;
    pcreate.rank_id=self.rank_id;
    [self.navigationController pushViewController:pcreate animated:YES];
    pcreate=nil;

}
-(void)initUI
{
    _tbView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.navHeight, self.view.bounds.size.width, self.view.bounds.size.height-self.navHeight) style:UITableViewStyleGrouped];
    _tbView.delegate = self;
    _tbView.dataSource = self;
    _tbView.backgroundColor =[UIColor whiteColor];
    _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tbView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_tbView];
}
#pragma mark - tableview
#pragma mark - tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
     return self.setcionArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return PHOTO_FRAME_WIDTH*2;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict =[self.setcionArray objectAtIndex:indexPath.section];
    return [ImageSettingCell returnCellHeight:dict];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ImageSettingCell *cell = (ImageSettingCell*)[tableView  dequeueReusableCellWithIdentifier:@""];
    if(cell == nil)
    {
        cell = [[ImageSettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
        __weak typeof (self) wSelf = self;
        [cell setCallBack:^(BEventType t, UIView *v,id obj,id key,NSIndexPath *indexPath) {
            [wSelf cellAction:t views:v obj:obj key:key indexPath:indexPath];
        }];
    }
    NSDictionary *dict =[self.setcionArray objectAtIndex:indexPath.section];
    [cell showInfo:dict key:@"ImageSettingCell" indexPath:indexPath];
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}
-(void)cellAction:(BEventType)type views:(UIView *)v obj:(id)obj key:(id)key indexPath:(NSIndexPath*)indexPath
{
    switch (type) {
        case BEventType_PTop:
        {
            NSMutableArray *array =[NSMutableArray arrayWithArray:self.setcionArray];
            for (int i=0;i<[array count];i++) {
                NSDictionary *dict =[array objectAtIndex:i];
                NSMutableDictionary *temp = [dict mutableCopy];
                
                [temp setValue:@"0" forKey:@"isTop"];
                [array replaceObjectAtIndex:i withObject:temp];
                temp=nil;
            }
            self.setcionArray =array;
            NSDictionary *dict =[self.setcionArray objectAtIndex:indexPath.section];
            NSMutableDictionary *temp = [dict mutableCopy];
            
            [temp setValue:[NSString stringWithFormat:@"%@",[obj objectForKey:@"width"]] forKey:@"width"];
            [temp setValue:[NSString stringWithFormat:@"%@",[obj objectForKey:@"height"]] forKey:@"height"];
            int isSelected =[[temp objectForKey:@"isTop"] intValue];
            if (isSelected==1) {
                [temp setValue:@"0" forKey:@"isTop"];
            }else{
                [temp setValue:@"1" forKey:@"isTop"];
                 self.top =[NSString stringWithFormat:@"%ld",(long)indexPath.section];
            }
            [self.setcionArray replaceObjectAtIndex:indexPath.section withObject:temp];
            temp=nil;
           
            [self.tbView reloadData];
        }
            break;
        case BEventType_PSelectd:
        {
            //
            NSDictionary *dict =[self.setcionArray objectAtIndex:indexPath.section];
            NSMutableDictionary *temp = [dict mutableCopy];
            int isSelected =[[temp objectForKey:@"isSelected"] intValue];
            [temp setValue:[NSString stringWithFormat:@"%@",[obj objectForKey:@"width"]] forKey:@"width"];
            [temp setValue:[NSString stringWithFormat:@"%@",[obj objectForKey:@"height"]] forKey:@"height"];
            if (isSelected==1) {
                [temp setValue:@"0" forKey:@"isSelected"];
            }else{
                [temp setValue:@"1" forKey:@"isSelected"];
            }
            [self.setcionArray replaceObjectAtIndex:indexPath.section withObject:temp];
            temp=nil;
            self.top =[NSString stringWithFormat:@"%ld",(long)indexPath.section];
            [self.tbView reloadData];
        }
            break;
        default:
            break;
    }
}

-(void)getData
{
    id data =[self.dictionary objectForKey:@"pictureinfolist"];
    if (data&&[data isKindOfClass:[NSArray class]]) {
        [self.setcionArray removeAllObjects];
        for (int i=0; i<[data count]; i++) {
            NSDictionary *dictt =[data objectAtIndex:i];
            NSMutableDictionary *dict =[NSMutableDictionary dictionary];
            [dict setValue:[dictt objectForKey:@"url"] forKey:@"url"];
            [dict setValue:@"0" forKey:@"width"];
            [dict setValue:@"0" forKey:@"height"];
            if (i==0) {
                [dict setValue:@"1" forKey:@"isSelected"];
                [dict setValue:@"1" forKey:@"isTop"];
            }else{
                [dict setValue:@"0" forKey:@"isSelected"];
                [dict setValue:@"0" forKey:@"isTop"];
            }
            
            [self.setcionArray addObject:dict];
        }
    }
}
@end
